package 曾世昌12_12;

import org.apache.commons.io.FileUtils;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class test {
    public static void main(String[] args) throws Exception {
        ArrayList<Class> clas = new ArrayList<>();
        List<Element> cla = new SAXReader().read( "E:\\untitled5\\src\\rc\\demo12_12\\classes.xml" ).getRootElement().elements( "classes" );
        List<Element> stu = new SAXReader().read( "E:\\untitled5\\src\\rc\\demo12_12\\student.xml" ).getRootElement().elements( "student" );
        for (Element element : cla) {
            ArrayList<Student> students = new ArrayList<>();
            for (Element element1 : stu) {
                if (element.attributeValue( "classID" ).equals( element1.attributeValue( "classID" ) )) {
                    students.add( new Student( element1.attributeValue( "number" ), element1.attributeValue( "name" ),
                            Integer.parseInt( element1.attributeValue( "age" ) ), element1.attributeValue( "gender" ),
                            Integer.parseInt( element1.attributeValue( "math" ) ), element1.attributeValue( "classID" ) ) );
                }
            }
            clas.add( new Class( students, element.attributeValue( "classID" ), element.attributeValue( "classname" ),
                    element.attributeValue( "teacher" ), element.attributeValue( "assistant" ),
                    element.attributeValue( "instructor" ) ) );
        }
        b( clas );
        a( clas );
        a1( clas );
        a2( clas );
        a3(clas);
    }

    public static void b(ArrayList<Class> clas) {
        int max = 0, rs = 0;
        Class a = clas.get( 0 );
        for (int i = 0; i < clas.size(); i++) {
            int maq = 0;
            for (int i1 = 0; i1 < clas.get( i ).stu.size(); i1++) {
                maq += clas.get( i ).stu.get( i1 ).getAge();
                rs = clas.get( i ).stu.size();
            }
            if (max < (maq / rs)) {
                max = (maq / rs);
                a = clas.get( i );
            }
        }
        System.out.println( a.getClassID() + "\t" + a.getClassname() + "\t" + max );
    }

    public static void a(ArrayList<Class> clas) {
        int min = 0, a = 0;
        Class mincla = clas.get( 0 );
        for (int i = 0; i < clas.size(); i++) {
            for (int i1 = 0; i1 < clas.get( i ).stu.size(); i1++) {
                min += clas.get( i ).stu.get( i1 ).getMath();
                a = clas.get( i ).stu.size();
            }
            if (min > min / a) {
                min = min / a;
                mincla = clas.get( i );
            }
        }
        System.out.println( mincla.getClassID() + "\t" + mincla.getClassname() + "\t" + min );
    }

    public static void a1(ArrayList<Class> clas) {
        int max = 0, a = 0;
        Class mincla = clas.get( 0 );
        String str = "";
        for (int i = 0; i < clas.size(); i++) {
            a = 0;
            for (int i1 = 0; i1 < clas.get( i ).stu.size(); i1++) {
                if (clas.get( i ).stu.get( i1 ).math < 60) {
                    a++;
                }
            }
            if (max < a) {
                max = a;
                mincla = clas.get( i );
            }
        }
        System.out.println( mincla.getClassID() + "\t" + mincla.getClassname() + "\t" + a );
    }

    public static void a2(ArrayList<Class> clas) {
        for (int i = 0; i < clas.size(); i++) {
            int a = 0;
            for (int i1 = 0; i1 < clas.get( i ).stu.size(); i1++) {
                if (clas.get( i ).stu.get( i1 ).getAge() < 18) {
                    a++;
                }
            }
            System.out.println( clas.get( i ).getClassname() + "\t" + a );
        }
    }public static void a3(ArrayList<Class> clas) throws IOException {
        for (Class aClass : clas) {
            aClass.getStu().sort(new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    if (o1.getAge() - o2.getAge() < 0) {
                        return -1;
                    } else if (o1.getAge() - o2.getAge() == 0 && o1.getMath() - o2.getMath() < 0) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
            });
        }
        for (Class aClass : clas) {
            FileUtils.writeLines(new File(aClass.getClassID()+".txt"),aClass.getStu());
        }
    }
}
